using UnityEngine;
using System.Collections;

public class SmoothScroll : MonoBehaviour 
{
	public float fResistanceFactor = 0.98f;
	public float fStopThreashold = 0.01f;
	
	Plane planeHit;
	Vector3 v3StartPos;
	Vector3 v3LastPos;
	Vector3 v3Delta;
	float fStartTime;
	
	bool bTranslating = false;
	
	void OnMouseDown()
	{
		bTranslating = false;
		v3StartPos = GetHitPoint ();
		v3LastPos = v3StartPos;
		fStartTime = Time.time;
    }
 
	void OnMouseDrag()
	{
		Vector3 v3T = GetHitPoint ();
		transform.Translate(v3T - v3LastPos);
		v3LastPos = v3T;
    }
 
	void OnMouseUp()
	{
		v3Delta = GetHitPoint();
		v3Delta = (v3Delta - v3StartPos) / (Time.time - fStartTime) * Time.deltaTime;
		bTranslating = true;
    }
 
    Vector3 GetHitPoint()
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		float fDist;
		
		if (planeHit.Raycast (ray, out fDist))
			return ray.GetPoint (fDist);
		else 
			return Vector3.zero;
	}
 
	void Start() 
	{
		planeHit =  new Plane(Vector3.forward, transform.position);
	}
	
	void Update () 
	{
		if (bTranslating) 
		{
			transform.position += v3Delta;
			v3Delta = v3Delta * fResistanceFactor;
			
			if (v3Delta.magnitude < fStopThreashold)
				bTranslating = false;
		}
	}
}
